package com.tzy.common.utils;

import java.util.Calendar;

public class UpPhotoNameUtils {
    public static String getPhotoName(String name, String imageFileName) {
        String fileName = name;
        Calendar cal = Calendar.getInstance();
        //如果年的目录不存在，创建年的目录
        int year = cal.get(Calendar.YEAR);
        fileName = fileName + "_" + year;
        //如果月份不存在，创建月份的目录
        int month = cal.get(Calendar.MONTH) + 1;
        fileName = fileName + "_";
        if (month < 10) {
            fileName = fileName + "0";
        }
        fileName = fileName + month;
        //生成文件名的日部分
        int day = cal.get(Calendar.DAY_OF_MONTH);
        fileName = fileName + "_";
        if (day < 10) {
            fileName = fileName + "0";
        }
        fileName = fileName + day;
        //生成文件名的小时部分
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        if (hour < 10) {
            fileName = fileName + "0";
        }
        fileName = fileName + hour;
        //生成文件名的分钟部分
        int minute = cal.get(Calendar.MINUTE);
        if (minute < 10) {
            fileName = fileName + "0";
        }
        fileName = fileName + minute;
        //生成文件名的秒部分
        int second = cal.get(Calendar.SECOND);
        if (second < 10) {
            fileName = fileName + "0";
        }
        fileName = fileName + second;
        //生成文件名的毫秒部分
        int millisecond = cal.get(Calendar.MILLISECOND);
        if (millisecond < 10) {
            fileName = fileName + "0";
        }
        if (millisecond < 100) {
            fileName = fileName + "0";
        }
        fileName = fileName + millisecond;
        //生成文件的扩展名部分,只截取最后单位
        String endName = imageFileName.substring(imageFileName.lastIndexOf("."));//截取之后的值
        fileName = fileName + endName;
        return fileName;
    }
//————————————————
//    版权声明：本文为CSDN博主「parsifal_cc」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//    原文链接：https://blog.csdn.net/m0_49647974/article/details/109557562
}
